<template>
  <div class="app-container">
    <van-nav-bar title="慰问品发放" left-arrow @click-left="$router.push('/home')" />
    <div class="content-box" v-loading="dataLoading">
      <img class="banner" :src="require('@/assets/images/condolenceDistribution/banner.png')" alt="">
      <div class="list-box">
        <div class="list-title">慰问品领取</div>
        <div class="list-content" v-if="condolenceItemList.length">
          <div class="list-item" v-for="(item, index) in condolenceItemList" :key="index" @click="toDetail(item)">
            <div class="item-cover">
              <img class="cover-img" :src="item.imageUrl">
            </div>
            <div class="item-label-box">
              <div class="item-label">
                <span class="text">{{ item.activityName }}</span>
                <el-tag :type="'primary'" effect="dark">{{'查看'}}</el-tag>
              </div>
              <div class="item-tip">
                {{ dayjs(item.startTime).format('YYYY年MM月DD日') }}—{{ dayjs(item.endTime).format('YYYY年MM月DD日') }}
              </div>
            </div>
          </div>
        </div>
        <div class="empty" v-else>暂无数据</div>
      </div>
      <div class="list-box">
        <div class="list-title">我的领取</div>
        <div class="list-content" v-if="myItemList.length">
          <div class="list-item" v-for="(item, index) in myItemList" :key="index" @click="toDetail(item)">
            <div class="item-cover">
              <img class="cover-img" :src="item.imageUrl">
            </div>
            <div class="item-label-box">
              <div class="item-label">
                <span class="text">{{ item.activityName }}</span>
                <el-tag :type="'primary'" effect="dark">{{ logisticsStatusObj[item.logisticsStatus] }}</el-tag>
              </div>
              <div class="item-tip">{{ item.materialDetail }}</div>
            </div>
          </div>
        </div>
        <div class="empty" v-else>暂无数据</div>
      </div>
    </div>
  </div>
</template>

<script>
import * as api from '@/api/condolenceManger/index.js';
import dayjs from 'dayjs';
import { mapGetters } from 'vuex';

export default {
  name: 'CondolenceDistributionIndex',
  data() {
    return {
      dataLoading: false,
      form: { title: '' },
      condolenceItemParams: { pageNum: 1, pageSize: 10 },
      condolenceItemTotal: 0,
      condolenceItemList: [],
      myItemList: [],
      logisticsStatusObj: { 0: '未发放', 1: '已发放' },
    };
  },
  computed: {
    ...mapGetters(['userId']),
  },
  created() {
    this.getData();
  },
  methods: {
    dayjs,
    getData() {
      this.dataLoading = true;
      api.getGiftList(this.queryParams).then((res) => {
        this.condolenceItemList = res.rows;
        this.condolenceItemTotal = res.total;
        api.getGiftRecordList({ userId: this.userId }).then((res) => {
          this.myItemList = res.rows.map((record) => {
            const condolenceItem = this.condolenceItemList.find((item) => item.collectId == record.collectId);
            record.activityName = condolenceItem.activityName;
            record.imageUrl = condolenceItem.imageUrl;
            return record;
          });
          this.dataLoading = false;
        });
      });
    },
    toDetail(item) {
      this.$router.push({ name: 'CondolenceDistributionDetail', query: { collectId: item.collectId } });
    },
  },
};
</script>

<style lang="scss" scoped>
@import '@/assets/styles/mixin.scss';

.content-box {
  width: calc(100% - 30px);
  height: calc(100% - 70px);
  padding: 10px 15px;
  display: flex;
  flex-direction: column;
  & > div {
    margin-bottom: 10px;
    &:last-child {
      margin-bottom: 0px;
    }
  }
}

.banner {
  width: 100%;
  margin-bottom: 10px;
}

@include list-box-style;

.list-box {
  width: calc(100% - 30px);
  flex: 1;

  .item-label {
    display: flex;
    align-items: center;
    .text {
      flex: 1;
    }
    ::v-deep .el-tag {
      flex-shrink: 0;
      height: 26px;
      line-height: 25px;
      border-radius: 14px;
      &.el-tag--primary {
        cursor: pointer;
      }
    }
  }
}
</style>
